﻿unit VerifyAcc;

interface

uses
  Winapi.Windows, Winapi.Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIFrame, DB, DBClient, ClientRowSet, uniScreenMask,
  uniBasicGrid, uniDBGrid, uniMultiItem, uniComboBox, uniDBComboBox,
  uniDBLookupComboBox, uniButton, uniBitBtn, uniDateTimePicker, uniLabel,
  uniGUIBaseClasses, uniPanel, Clipbrd, uniEdit;

type
  TFrmVerifyAcc = class(TUniFrame)
    UniPanel1: TUniPanel;
    UniLabel1: TUniLabel;
    edStartDate: TUniDateTimePicker;
    btFind: TUniBitBtn;
    UniLabel2: TUniLabel;
    edCloseDate: TUniDateTimePicker;
    UniLabel4: TUniLabel;
    cbNode: TUniDBLookupComboBox;
    gridOrder: TUniDBGrid;
    UniPanel3: TUniPanel;
    UniBitBtn1: TUniBitBtn;
    UniBitBtn2: TUniBitBtn;
    UniBitBtn3: TUniBitBtn;
    btExport: TUniBitBtn;
    dsOrder: TDataSource;
    crsOrder: TClientRowSet;
    crsOrderORDER_ID: TStringField;
    crsOrderCON_ORDID: TStringField;
    crsOrderBILL_NO: TStringField;
    crsOrderORDER_NO: TStringField;
    crsOrderORDER_TYPE: TStringField;
    crsOrderTSP_STATE: TIntegerField;
    crsOrderACPT_STATE: TIntegerField;
    crsOrderCARGO: TStringField;
    crsOrderCARGO_CNT: TIntegerField;
    crsOrderWEIGHT: TFloatField;
    crsOrderVOLUME: TStringField;
    crsOrderDISTANCE: TFloatField;
    crsOrderCOD: TFloatField;
    crsOrderCOD_FEE: TFloatField;
    crsOrderCOD_BLCID: TStringField;
    crsOrderCOD_STATE: TIntegerField;
    crsOrderASK_RTBILL: TIntegerField;
    crsOrderSEND_PNTNO: TStringField;
    crsOrderSEND_GROUP: TStringField;
    crsOrderSEND_NODE: TStringField;
    crsOrderSEND_NAME: TStringField;
    crsOrderSEND_TEL: TStringField;
    crsOrderSEND_CONT: TStringField;
    crsOrderSEND_CITY: TStringField;
    crsOrderSEND_ADDR: TStringField;
    crsOrderDEST_PNTNO: TStringField;
    crsOrderDEST_GROUP: TStringField;
    crsOrderDEST_NODE: TStringField;
    crsOrderDEST_NAME: TStringField;
    crsOrderDEST_TEL: TStringField;
    crsOrderDEST_CONT: TStringField;
    crsOrderDEST_CITY: TStringField;
    crsOrderDEST_ADDR: TStringField;
    crsOrderRECEIVER: TStringField;
    crsOrderTSP_ACCNO: TStringField;
    crsOrderTSP_PNTNO: TStringField;
    crsOrderTSP_GROUP: TStringField;
    crsOrderTSP_NODE: TStringField;
    crsOrderTSP_NAME: TStringField;
    crsOrderTSP_EMPTEL: TStringField;
    crsOrderTRUCK_NO: TStringField;
    crsOrderTSP_TEL: TStringField;
    crsOrderTSP_EMPNO: TStringField;
    crsOrderTSP_EMPNAME: TStringField;
    crsOrderTSP_USRNO: TStringField;
    crsOrderFETCH_NODE: TStringField;
    crsOrderFETCH_EMPNO: TStringField;
    crsOrderFETCH_EMPNAME: TStringField;
    crsOrderFETCH_EMPTEL: TStringField;
    crsOrderFETCH_NODE_INC: TFloatField;
    crsOrderDISP_NODE: TStringField;
    crsOrderDISP_EMPNO: TStringField;
    crsOrderDISP_EMPNAME: TStringField;
    crsOrderDISP_EMPTEL: TStringField;
    crsOrderDISP_NODE_INC: TFloatField;
    crsOrderRECBILL_NAME: TStringField;
    crsOrderFEE_BLCID: TStringField;
    crsOrderFEE: TFloatField;
    crsOrderPAYSIDE: TIntegerField;
    crsOrderADV_FEE: TFloatField;
    crsOrderINUS_FEE: TFloatField;
    crsOrderFEE_STATE: TIntegerField;
    crsOrderDLIV_PAYIN: TFloatField;
    crsOrderBACK_FEE: TFloatField;
    crsOrderCRT_DATE: TStringField;
    crsOrderSND_DATE: TStringField;
    crsOrderARV_DATE: TStringField;
    crsOrderRCV_TIME: TStringField;
    crsOrderEXCEPT_INFO: TStringField;
    crsOrderREMARK: TStringField;
    crsOrderGROUP_CODE: TStringField;
    crsOrderNODE_NO: TStringField;
    crsOrderNODE_NAME: TStringField;
    crsOrderSYS_ACCOUNT: TStringField;
    crsOrderUPDATETIME: TStringField;
    crsOrderVERSION: TLargeintField;
    crsOrderCHOOSED: TBooleanField;
    crsOrderCUST_NAME: TStringField;
    crsOrderFEESTATE: TStringField;
    crsOrderRECEIVEABLE: TFloatField;
    crsOrderPAYMODE: TStringField;
    dscust: TDataSource;
    dsNode: TDataSource;
    dsEmp: TDataSource;
    btNewBlcBill: TUniBitBtn;
    crsOrderFETCH_BLCID: TStringField;
    crsOrderDISP_BLCID: TStringField;
    crsOrderBALANCE: TFloatField;
    crsOrderBLC_NODE: TStringField;
    crsOrderSTATE: TStringField;
    crsOrderNODE_INC: TFloatField;
    UniPanel2: TUniPanel;
    crsOrderBUSI_TYPE: TStringField;
    crsNodeBlc: TClientRowSet;
    crsNodeBlcID: TStringField;
    crsNodeBlcGROUP_CODE: TStringField;
    crsNodeBlcNODE_NO: TStringField;
    crsNodeBlcNODE_NAME: TStringField;
    crsNodeBlcBLC_DATE: TStringField;
    crsNodeBlcSTART_DATE: TStringField;
    crsNodeBlcCLOSE_DATE: TStringField;
    crsNodeBlcFETC_CNT: TLargeintField;
    crsNodeBlcDISP_CNT: TLargeintField;
    crsNodeBlcFETC_INC: TFloatField;
    crsNodeBlcDISP_INC: TFloatField;
    crsNodeBlcHAND_IN: TFloatField;
    crsNodeBlcDEDUCT: TFloatField;
    crsNodeBlcBLC_STATE: TSmallintField;
    crsNodeBlcSYS_ACCOUNT: TStringField;
    crsNodeBlcBLC_NO: TStringField;
    InfoFind: TUniScreenMask;
    crsOrderOP_FEE: TFloatField;
    pnlClose: TUniPanel;
    btClose: TUniBitBtn;
    crsOrderDISP_FEE: TFloatField;
    crsNode: TClientRowSet;
    UniLabel3: TUniLabel;
    lblFee: TUniLabel;
    UniLabel5: TUniLabel;
    lblinsFee: TUniLabel;
    UniLabel6: TUniLabel;
    lblAdvFee: TUniLabel;
    UniLabel7: TUniLabel;
    lblCod: TUniLabel;
    lblCodFee: TUniLabel;
    UniLabel9: TUniLabel;
    lblReceivable: TUniLabel;
    UniLabel10: TUniLabel;
    lblNodeInc: TUniLabel;
    UniLabel11: TUniLabel;
    lblBalance: TUniLabel;
    crsOrderPROC_STATE: TStringField;
    edtBillNo: TUniEdit;
    UniLabel12: TUniLabel;
    UniLabel13: TUniLabel;
    lblCount: TUniLabel;
    UniPanel9: TUniPanel;
    pnlCod: TUniPanel;
    UniPanel4: TUniPanel;
    pnRecvable: TUniPanel;
    UniPanel6: TUniPanel;
    pnPayable: TUniPanel;
    UniPanel8: TUniPanel;
    pnBalance: TUniPanel;
    UniPanel5: TUniPanel;
    pnopFee: TUniPanel;
    UniLabel14: TUniLabel;
    lblCodMonth: TUniLabel;
    UniLabel15: TUniLabel;
    lblCodDaySelf: TUniLabel;
    UniLabel16: TUniLabel;
    lblCodDayOther: TUniLabel;
    UniLabel17: TUniLabel;
    UniLabel18: TUniLabel;
    UniLabel19: TUniLabel;
    lblFeeFetch: TUniLabel;
    lblFeeDisp: TUniLabel;
    lblFeeMonth: TUniLabel;
    UniLabel20: TUniLabel;
    lblFeeSum: TUniLabel;
    UniLabel21: TUniLabel;
    UniLabel22: TUniLabel;
    lblOtherFeeSum: TUniLabel;
    procedure UniFrameCreate(Sender: TObject);
    procedure btFindClick(Sender: TObject);
    procedure crsOrderCalcFields(DataSet: TDataSet);
    procedure UniBitBtn3Click(Sender: TObject);
    procedure UniBitBtn1Click(Sender: TObject);
    procedure UniBitBtn2Click(Sender: TObject);
    procedure btExportClick(Sender: TObject);
    procedure btNewBlcBillClick(Sender: TObject);
    procedure cbNodeCloseUp(Sender: TObject);
    procedure gridOrderColumnSort(Column: TUniDBGridColumn; Direction: Boolean);
    procedure gridOrderCellClick(Column: TUniDBGridColumn);
    procedure gridOrderDblClick(Sender: TObject);
    procedure cbNodeKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure btCloseClick(Sender: TObject);
  private
    { Private declarations }
    fetchcnt, dispcnt: integer;
    fetchinc, dispinc,handin,deduct: Double;
    opFeeFunc, dispFeeFunc: string;
    currField: TField;
    procedure ChooseAll(choosed: Boolean);
    procedure NewNodeInc(ID: String);
  public
    { Public declarations }
    procedure showBalance;
    procedure UpdateNodeInc;
  end;

implementation

uses ExpModule, Main, uCommFunction, ToolModule, MessageBox, DictModule;
{$R *.dfm}
{ TFrmVerifyAcc }

procedure TFrmVerifyAcc.btCloseClick(Sender: TObject);
begin
  MainForm.CloseOneTab(Self.Hint);
  //MainForm.CloseOneTab('中心对账');
end;

procedure TFrmVerifyAcc.btExportClick(Sender: TObject);
var
  FileName: String;
begin
  if not crsOrder.Active then
    exit;
  if crsOrder.RecordCount > 0 then
  begin
    FileName := 'files\' + cbNode.Text + edStartDate.Text + '-结算单.csv';
    Mainform.ExportExcel('', gridOrder, FileName);
  end
  else
  begin
    FrmMessageBox.ShowInfo(GetShowMsg('0038', '当前没有记录，不能导出空的数据文件'));
  end;

end;

procedure TFrmVerifyAcc.btFindClick(Sender: TObject);
begin
  if Trim(cbNode.KeyValue) = '' then
  begin
    FrmMessageBox.ShowInfo(GetShowMsg('0145','请选择结算站点!'));
    Exit;
  end;
  self.showBalance;
end;

procedure TFrmVerifyAcc.ChooseAll(choosed: Boolean);
var
  I: integer;
  payable, rcvable, opFee: Double;
  fee, insFee, advFee, cod, codFee, receivable, nodeInc, balance, codMonth, codDaySelf, codDayOther: Currency;
  feeFetch, feeDisp, feeMonth: Currency;
begin
  pnRecvable.Caption := '0';
  pnPayable.Caption := '0';
  pnBalance.Caption := '0';
  pnopFee.Caption := '0';
  pnlCod.Caption := '0';

  lblFee.Caption := '0';
  lblinsFee.Caption := '0';
  lblAdvFee.Caption := '0';
  lblCod.Caption := '0';
  lblCodFee.Caption := '0';
  lblReceivable.Caption := '0';
  lblNodeInc.Caption := '0';
  lblBalance.Caption := '0';
  lblCodMonth.Caption := '0';
  lblCodDaySelf.Caption := '0';
  lblCodDayOther.Caption := '0';
  lblFeeFetch.Caption := '';
  lblFeeDisp.Caption := '';
  lblFeeMonth.Caption := '';
  lblFeeSum.Caption := '';
  lblOtherFeeSum.Caption := '';

  rcvable := strtofloat(pnRecvable.Caption);
  payable := strtofloat(pnPayable.Caption);
  balance := strtofloat(pnBalance.Caption);
  opFee := StrToFloat(pnopFee.Caption);

  fee := 0;
  insFee := 0;
  advFee := 0;
  cod := 0;
  codFee := 0;
  receivable := 0;
  nodeInc := 0;
  balance := 0;
  codMonth := 0;
  codDaySelf := 0;
  codDayOther := 0;
  feeFetch := 0;
  feeDisp := 0;
  feeMonth := 0;

  crsOrder.First;
  for I := 1 to crsOrder.RecordCount do
  begin
    crsOrder.Edit;
    //if (crsOrder.FieldByName('STATE').AsString = '未记账') and
    //  (crsOrder.FieldByName('NODE_INC').AsFloat > 0) then
    if (crsOrder.FieldByName('STATE').AsString = '未记账') and (choosed) then
    begin
      rcvable := payable + crsOrder.FieldByName('RECEIVEABLE').AsFloat;
      payable := payable + crsOrder.FieldByName('NODE_INC').AsFloat;
      opFee := opFee + crsOrder.FieldByName('op_fee').AsFloat;
      crsOrder.FieldByName('choosed').AsBoolean := choosed;

      if (crsOrder.FieldByName('fetch_node').AsString = cbNode.KeyValue) and (crsOrder.FieldByName('payside').AsInteger in [1, 3])
        and (crsOrder.FieldByName('crt_date').AsString >= edStartDate.Text) and (crsOrder.FieldByName('crt_date').AsString <= edCloseDate.Text + ' 23:59:59') then
      begin
        fee := fee + crsOrder.FieldByName('fee').AsFloat;
        insFee := insFee + crsOrder.FieldByName('inus_fee').AsFloat;
        advFee := advFee + crsOrder.FieldByName('adv_fee').AsFloat;
        if crsOrder.FieldByName('payside').AsInteger = 1 then
          feeFetch := feeFetch + crsOrder.FieldByName('fee').AsFloat
        else
        if crsOrder.FieldByName('payside').AsInteger = 3 then
          feeMonth := feeMonth + crsOrder.FieldByName('fee').AsFloat;
      end
      else
      if (crsOrder.FieldByName('disp_node').AsString = cbNode.KeyValue) and (crsOrder.FieldByName('payside').AsInteger in [2, 4]) and (crsOrder.FieldByName('tsp_state').AsInteger = 9)
        and (crsOrder.FieldByName('rcv_time').AsString >= edStartDate.Text) and (crsOrder.FieldByName('rcv_time').AsString <= edCloseDate.Text + ' 23:59:59') then
      begin
        fee := fee + crsOrder.FieldByName('fee').AsFloat;
        insFee := insFee + crsOrder.FieldByName('inus_fee').AsFloat;
        advFee := advFee + crsOrder.FieldByName('adv_fee').AsFloat;
        feeDisp := feeDisp + crsOrder.FieldByName('fee').AsFloat;
      end;
      if (crsOrder.FieldByName('disp_node').AsString = cbNode.KeyValue) and (crsOrder.FieldByName('tsp_state').AsInteger = 9)
        and (crsOrder.FieldByName('rcv_time').AsString >= edStartDate.Text) and (crsOrder.FieldByName('rcv_time').AsString <= edCloseDate.Text + ' 23:59:59') then
      begin
        cod := cod + crsOrder.FieldByName('cod').AsFloat;
        if crsOrder.FieldByName('payside').AsInteger = 3  then
          codMonth := codMonth + crsOrder.FieldByName('cod').AsFloat
        else
        begin
          if crsOrder.FieldByName('fetch_node').AsString = cbNode.KeyValue then
            codDaySelf := codDaySelf + crsOrder.FieldByName('cod').AsFloat
          else
            codDayOther := codDayOther + crsOrder.FieldByName('cod').AsFloat;
        end;
        codFee := codFee + crsOrder.FieldByName('cod_fee').AsFloat;
      end;
      receivable := receivable + crsOrder.FieldByName('receiveable').AsFloat;
      nodeInc := nodeInc + crsOrder.FieldByName('node_inc').AsFloat;
      balance := balance + crsOrder.FieldByName('balance').AsFloat;
    end
    else
    if choosed = false then
      crsOrder.FieldByName('choosed').AsBoolean := false;
    crsOrder.Post;
    crsOrder.Next;
  end;

  //if choosed then
  //begin
    pnRecvable.Caption := floattostr(rcvable);
    pnPayable.Caption := floattostr(payable);
    pnBalance.Caption := floattostr(balance);
    pnopFee.Caption := FloatToStr(opFee);
  //end;

  lblFee.Caption := FloatToStr(fee);
  lblinsFee.Caption := FloatToStr(insFee);
  lblAdvFee.Caption := FloatToStr(advFee);
  lblCod.Caption := FloatToStr(cod);
  lblCodFee.Caption := FloatToStr(codFee);
  lblReceivable.Caption := FloatToStr(receivable);
  lblNodeInc.Caption := FloatToStr(nodeInc);
  lblBalance.Caption := FloatToStr(balance);
  lblCodMonth.Caption := FloatToStr(codMonth);
  lblCodDaySelf.Caption := FloatToStr(codDaySelf);
  lblCodDayOther.Caption := FloatToStr(codDayOther);
  lblFeeFetch.Caption := FloatToStr(feeFetch);
  lblFeeDisp.Caption := FloatToStr(feeDisp);
  lblFeeMonth.Caption := FloatToStr(feeMonth);
  lblFeeSum.Caption := FloatToStr(feeFetch + feeDisp);
  lblOtherFeeSum.Caption := FloatToStr(insFee + advFee + codFee);
  lblReceivable.Caption := FloatToStr(feeFetch + feeDisp + cod + insFee + advFee + codFee);
  lblBalance.Caption := FloatToStr(feeFetch + feeDisp + cod + insFee + advFee + codFee - nodeInc);
  //fee + advfee + inusfee
end;

procedure TFrmVerifyAcc.crsOrderCalcFields(DataSet: TDataSet);
var
  fee, advfee, inusfee, receive, nodeinc, opFee, dispFee, codFee, cod: Double;
  payside: integer;
  fetcnode, dispnode, blcnode: String;
begin
  fee := 0;
  advfee := 0;
  inusfee := 0;  //保价费寄件站点的支出，要交中心站点
  receive := 0;
  nodeinc := 0;
  opFee := 0;  //寄件件点的支出
  dispFee := 0; //寄件站点的支出,派件的收入

  fetcnode := DataSet.FieldByName('FETCH_NODE').AsString;
  dispnode := DataSet.FieldByName('DISP_NODE').AsString;
  payside  := DataSet.FieldByName('PAYSIDE').AsInteger;
  fee      := DataSet.FieldByName('FEE').AsFloat;
  advfee   := DataSet.FieldByName('ADV_FEE').AsFloat;
  inusfee  := DataSet.FieldByName('INUS_FEE').AsFloat;
  codFee := DataSet.FieldByName('cod_fee').AsFloat;
  cod := DataSet.FieldByName('cod').AsFloat;
  {opFeeFunc := dmtool.GetFunctionName(DataSet,'操作费');
  if opFeeFunc <> '' then
  begin
    opFee := dmtool.CalcBillFee(opFeeFunc, DataSet.FieldByName('weight').AsFloat,
      DataSet.FieldByName('fee').AsFloat,
      DataSet.FieldByName('inus_fee').AsFloat,
      DataSet.FieldByName('adv_fee').AsFloat,
      DataSet.FieldByName('cod').AsFloat,
      DataSet.FieldByName('cod_fee').AsFloat,
      DataSet.FieldByName('cargo_ctn').AsFloat);
  end;
  dispFeeFunc := dmtool.GetFunctionName(DataSet,'派件费');
  if dispFeeFunc <> '' then
  begin
    dispFee := dmtool.CalcBillFee(dispFeeFunc, DataSet.FieldByName('weight').AsFloat,
      DataSet.FieldByName('fee').AsFloat,
      DataSet.FieldByName('inus_fee').AsFloat,
      DataSet.FieldByName('adv_fee').AsFloat,
      DataSet.FieldByName('cod').AsFloat,
      DataSet.FieldByName('cod_fee').AsFloat,
      DataSet.FieldByName('cargo_ctn').AsFloat);
  end; }

  blcnode := cbNode.KeyValue;
  DataSet.FieldByName('BLC_NODE').AsString := blcnode; // 结算站点

  if ((payside in [1, 3]) and (fetcnode = blcnode)) or
    ((payside in [2, 4]) and (dispnode = blcnode)) then // 寄件寄付或到件到付，运费交中心
  begin                                                 // 到付， 所有费用都是派件站点应缴
    if (payside in [1, 3]) and (crsOrder.FieldByName('crt_date').AsString >= edStartDate.Text) and (crsOrder.FieldByName('crt_date').AsString <= edCloseDate.Text + ' 23:59:59') then
      receive := fee + advfee + inusfee
    else
    if (payside in [2, 4]) and (crsOrder.FieldByName('tsp_state').AsInteger = 9) and (crsOrder.FieldByName('rcv_time').AsString >= edStartDate.Text) and (crsOrder.FieldByName('rcv_time').AsString <= edCloseDate.Text + ' 23:59:59') then  //到付要以签收为基准
      receive := fee + advfee + inusfee;
  end
  else
    receive := 0;

  if (fetcnode = blcnode) then // 若结算站点为寄件站点则取寄件提成
  begin
    DataSet.FieldByName('BUSI_TYPE').AsString := '收件';
    nodeinc := DataSet.FieldByName('FETCH_NODE_INC').AsFloat;

    if DataSet.FieldByName('FETCH_BLCID').AsString = '' then
      DataSet.FieldByName('STATE').AsString := '未记账'
    else
      DataSet.FieldByName('STATE').AsString := '已记账';
  end;

  if (dispnode = blcnode) then // 若结算站点为派件站点则取到件提成
  begin
    DataSet.FieldByName('BUSI_TYPE').AsString := '派件';
    nodeinc := nodeinc + DataSet.FieldByName('DISP_NODE_INC').AsFloat;

    if DataSet.FieldByName('DISP_BLCID').AsString = '' then
      DataSet.FieldByName('STATE').AsString := '未记账'
    else
      DataSet.FieldByName('STATE').AsString := '已记账';
  end;
  //如果是月结客户， 运费之类则不计算应缴，只计算代收货款
  if (dispnode = blcnode) and (DataSet.FieldByName('tsp_state').AsInteger = 9) and (crsOrder.FieldByName('rcv_time').AsString >= edStartDate.Text) and (crsOrder.FieldByName('rcv_time').AsString <= edCloseDate.Text + ' 23:59:59') then
  begin
    receive := receive + cod + codFee;
  end;

  DataSet.FieldByName('RECEIVEABLE').AsFloat := receive;
  DataSet.FieldByName('NODE_INC').AsFloat := nodeinc;
  DataSet.FieldByName('BALANCE').AsFloat := receive - nodeinc; // 中心应收款等于运费减去提成
  DataSet.FieldByName('op_fee').AsFloat := opFee;
end;


procedure TFrmVerifyAcc.gridOrderCellClick(Column: TUniDBGridColumn);
begin
  currField := Column.Field;
end;

procedure TFrmVerifyAcc.gridOrderColumnSort(Column: TUniDBGridColumn;
  Direction: Boolean);
begin
  SortColumn((gridOrder.DataSource.DataSet as TClientRowSet), Column.FieldName, Direction);
end;

procedure TFrmVerifyAcc.gridOrderDblClick(Sender: TObject);
begin
  if currField <> nil then
    Clipboard.AsText := currField.AsString;
end;

procedure TFrmVerifyAcc.NewNodeInc(ID: String);
var
  sysaccno,blcdate,blcno: string;
begin
  sysaccno := dsNode.DataSet.FieldByName('SYS_ACCOUNT').AsString;
  blcdate := formatdatetime('yyyy-MM-dd',date);
  blcno := cbNode.KeyValue + '-' + formatdatetime('yyMMdd',edStartDate.DateTime)
         + '-' + formatdatetime('yyMMdd',edStartDate.DateTime);
  crsNodeBlc.Append;
  crsNodeBlc.FieldByName('ID').AsString := ID;
  crsNodeBlc.FieldByName('BLC_NO').AsString := blcno;
  crsNodeBlc.FieldByName('START_DATE').AsString := edStartDate.Text;
  crsNodeBlc.FieldByName('CLOSE_DATE').AsString := edCloseDate.Text;
  crsNodeBlc.FieldByName('NODE_NO').AsString := cbNode.KeyValue;
  crsNodeBlc.FieldByName('NODE_NAME').AsString := cbNode.Text;
  crsNodeBlc.FieldByName('GROUP_CODE').AsString := dmexpress.UserInfo.GroupCode;
  crsNodeBlc.FieldByName('SYS_ACCOUNT').AsString := sysaccno;

  crsNodeBlc.FieldByName('BLC_DATE').AsString := blcdate;
  crsNodeBlc.FieldByName('HAND_IN').AsFloat := self.handin; // 上缴金额
  crsNodeBlc.FieldByName('DEDUCT').AsFloat := self.deduct;  // 提成

  crsNodeBlc.FieldByName('FETC_CNT').AsInteger := self.fetchcnt;
  crsNodeBlc.FieldByName('DISP_CNT').AsInteger := self.dispcnt;
  crsNodeBlc.FieldByName('FETC_INC').AsFloat := self.fetchinc;
  crsNodeBlc.FieldByName('DISP_INC').AsFloat := self.dispinc;
  crsNodeBlc.ApplyUpdates;

end;

procedure TFrmVerifyAcc.showBalance;
var
  orderid, sqltxt, stdate, cldate, condition: String;
begin
  stdate := formatdatetime('yyyy-MM-dd', edStartDate.DateTime);
  cldate := formatdatetime('yyyy-MM-dd', edCloseDate.DateTime) + ' 24:00:00';

  {condition := 'CRT_DATE>=' + quotedstr(stdate)
     + ' and CRT_DATE<=' + quotedstr(cldate)
     + ' and TSP_GROUP=' + quotedstr(dmexpress.UserInfo.GroupCode);  }
  condition := condition + ' AND tsp_group=' + QuotedStr(dmExpress.UserInfo.GroupCode);
  {if cbNode.Text <> '' then
    condition := condition +  ' and (fetch_node=' + quotedstr(cbNode.KeyValue)
     + ' or   disp_node=' + quotedstr(cbNode.KeyValue) + ')';
  {condition := condition +' AND ((crt_date>=' + QuotedStr(stdate) + ' AND crt_date<=' + QuotedStr(cldate) +
    ' AND payside IN (1,3) AND tsp_state<>0) ' +
    ' OR (rcv_time>=' + QuotedStr(stdate) + ' AND rcv_time<=' + QuotedStr(cldate) + ' AND payside IN (2,4) AND tsp_state=9))';  }
  condition := condition +' AND ((crt_date>=' + QuotedStr(stdate) + ' AND crt_date<=' + QuotedStr(cldate) +
    ' AND tsp_state<>0 and payside IN (1,3) AND fetch_node=' + QuotedStr(cbNode.KeyValue) + ')' +
    ' OR (rcv_time>=' + QuotedStr(stdate) + ' AND rcv_time<=' + QuotedStr(cldate) + ' AND tsp_state=9  AND disp_node=' + QuotedStr(cbNode.KeyValue) + '))';
  if Trim(edtBillNo.Text) <> '' then
    condition := condition + ' AND bill_no=' + QuotedStr(edtBillNo.Text);

  sqltxt := 'select * from tsp_order where (1=1) ' + condition + ' ORDER BY bill_no';
  crsOrder.Close;
  crsOrder.CommandText := sqltxt;
  crsOrder.Open;
  crsOrder.Filter := '';

  pnRecvable.Caption := '0';
  pnPayable.Caption := '0';
  pnBalance.Caption := '0';
  fetchcnt := 0; // 收件票数
  dispcnt := 0; // 派件票数
  fetchinc := 0; // 收件提成
  dispinc := 0; // 派件提成

  btNewBlcBill.Enabled := crsOrder.RecordCount > 0;
  crsOrder.Filter := '';
  gridOrder.OnColumnSort := gridOrderColumnSort;
  self.ChooseAll(false);
  lblCount.Caption := IntToStr(crsOrder.RecordCount);
end;

procedure TFrmVerifyAcc.UniBitBtn1Click(Sender: TObject);
begin
  self.ChooseAll(true);
end;

procedure TFrmVerifyAcc.UniBitBtn2Click(Sender: TObject);
begin
  self.ChooseAll(false);
end;

procedure TFrmVerifyAcc.UniBitBtn3Click(Sender: TObject);
begin
  UpdateNodeInc;
end;

procedure TFrmVerifyAcc.btNewBlcBillClick(Sender: TObject);
var
  I: integer;
  BLCID,NodeNo: String;
begin
  if (not crsOrder.Active) or (crsOrder.RecordCount = 0) then
    Exit;
  if crsOrder.FieldByName('blc_node').AsString <> cbNode.KeyValue then
  begin
    FrmMessageBox.ShowInfo(GetShowMsg('0039', '重选站点后，要刷新数据后才能进行该操作'));
    exit;
  end;

  self.fetchcnt := 0; // 收件票数
  self.dispcnt := 0; // 派件票数
  self.fetchinc := 0; // 收件提成
  self.dispinc := 0; // 派件提成
  self.handin  := 0;
  self.deduct := 0;

  if not crsNodeBlc.Active then
  begin
    crsNodeBlc.Close;
    crsNodeBlc.CommandText := 'select * from tsp_balance where 1=2';
    crsNodeBlc.Open;
  end;
  BLCID := getguid;

  crsOrder.First;
  for I := 1 to crsOrder.RecordCount do
  begin
    NodeNo := crsorder.FieldByName('BLC_NODE').AsString;
    crsOrder.Edit;
    if (crsorder.FieldByName('choosed').AsBoolean) and (crsorder.FieldByName('STATE').AsString = '未记账') then
    begin
      if crsOrder.FieldByName('fetch_node').AsString = NodeNo then
      begin
        crsOrder.FieldByName('FETCH_BLCID').AsString := BLCID;
        self.fetchcnt := self.fetchcnt + 1;
        self.fetchinc := self.fetchinc + crsOrder.FieldByName('FETCH_NODE_INC').AsFloat;
      end;

      if crsOrder.FieldByName('disp_node').AsString = NodeNo then
      begin
        crsOrder.FieldByName('DISP_BLCID').AsString := BLCID;
        self.dispcnt := dispcnt + 1;
        self.dispinc := dispinc + crsOrder.FieldByName('DISP_NODE_INC').AsFloat;
      end;

      self.handin := self.handin + crsOrder.FieldByName('RECEIVEABLE').AsFloat;
      self.deduct := self.deduct + crsOrder.FieldByName('NODE_INC').AsFloat;
    end;
    crsOrder.Post;
    crsOrder.Next;
  end;

  if crsOrder.RecordCount > 0 then
  begin
    self.NewNodeInc(BLCID);
    crsOrder.ApplyUpdates;
  end;

  self.ChooseAll(false);

end;

procedure TFrmVerifyAcc.cbNodeCloseUp(Sender: TObject);
begin
  btNewBlcBill.Enabled := false;
end;

procedure TFrmVerifyAcc.cbNodeKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key in [VK_DELETE, VK_BACK] then
  begin
    cbNode.ItemIndex := -1;
    cbNode.KeyValue := '';
  end;
end;

procedure TFrmVerifyAcc.UniFrameCreate(Sender: TObject);
begin
  SetCompoentLanguage(Self);
  crsNode.Data := dmDict.crsNode.Data;
  edStartDate.DateTime := date - 30;
  edCloseDate.DateTime := date;
  cbNode.KeyValue := dmexpress.UserInfo.nodeno;
  cbNode.Enabled := dmexpress.UserInfo.IsCenter;
  SetFieldsSort(gridOrder);
  currField := nil;
  gridOrder.OnColumnSort := nil;
  pnlClose.Visible := dmExpress.UserInfo.isPcModel;

  if dmExpress.UserInfo.LanguageType = 'ENG' then
    crsOrder.FieldByName('proc_state').LookupResultField := 'CAPTIONENG';
end;

procedure TFrmVerifyAcc.UpdateNodeInc;
var
  I: integer;
  weight,fee,advfee,inusfee,cod,codfee,cnt: Double;
  funcname:String;
begin
  crsOrder.First;
  for I := 1 to crsOrder.RecordCount do
  begin
    weight := crsOrder.FieldByName('weight').AsFloat;
    fee := crsOrder.FieldByName('fee').AsFloat;
    advfee := crsOrder.FieldByName('adv_fee').AsFloat;
    inusfee := crsOrder.FieldByName('INUS_FEE').AsFloat;
    cod := crsOrder.FieldByName('cod').AsFloat;
    codfee := crsOrder.FieldByName('cod_fee').AsFloat;
    cnt := crsOrder.FieldByName('CARGO_CNT').AsFloat;

    crsOrder.Edit;
    funcname := dmtool.GetFunctionName(crsorder,'收件站提成', crsOrder.FieldByName('fetch_node').AsString);
    crsOrder.FieldByName('FETCH_NODE_INC').AsFloat := dmtool.CalcBillFee(funcname,weight,fee,inusfee,advfee,cod,codfee,cnt);
    funcname := dmtool.GetFunctionName(crsorder,'派件站提成', crsOrder.FieldByName('disp_node').AsString);
    crsOrder.FieldByName('DISP_NODE_INC').AsFloat := dmtool.CalcBillFee(funcname,weight,fee,inusfee,advfee,cod,codfee,cnt);
    crsOrder.Post;
    crsOrder.Next;
  end;
  crsOrder.ApplyUpdates;
end;

end.
